#include <stdio.h>
#include "key.h"

struct KEY_T	*KeyList = NULL;
struct KEY_T	Button[KEY_MAX];


FifoForKey_t	FifoForKey;

void FifoFuncRegister(FifoLenForKey FifoLen, FifoPutForKey FifoPut, FifoGetForKey FifoGet)
{
	FifoForKey.Len	= FifoLen;
	FifoForKey.Put	= FifoPut;
	FifoForKey.Get	= FifoGet;
}

void KeyAdd(KEY_TYPE index, unsigned char ischecklongpush, unsigned char ischeckrepeat, IsKeyDownFunc fuc)
{	
	struct KEY_T *key;
	if(fuc == NULL) return;
	
	key = &Button[index]; 
	
	key->LongCount	= 0;
	key->LongTimeLimit = 0;
	key->ShortCount = KEY_FILTER_TIME / 2;
	key->ShortTimeLimit = KEY_FILTER_TIME;
	key->RepeatCount = 0;
	key->RepeatTimeLimit = 0;
	key->KeyInfo.Index = index;
	key->KeyInfo.State = 0;
	key->Fuc = fuc;
	if(ischecklongpush)
	{
		key->LongTimeLimit = KEY_LONG_TIME;
	}
	if(ischeckrepeat)
	{
		key->RepeatTimeLimit = 10;
	}
	key->next = KeyList;
	KeyList = key;
}

//按键状态检测
void KeyCheck(struct KEY_T *key)
{
	//相关注册按键状态反馈为1 按键按下
	if(key->Fuc())
	{
		//按键按下 滤波计数器向上计时   
		// 滤波器计数器  = 5  
		// 按下监测有效计数在 5 ~ 10，若小于5则强行设置为5
		// 若小于10，则累加，直到10s，开始处理；
		if (key->ShortCount < key->ShortTimeLimit)	
		{
			key->ShortCount = key->ShortTimeLimit;
		}
		else if(key->ShortCount < 2 * key->ShortTimeLimit)
		{
			key->ShortCount++;
		}
		else
		{
			if (key->KeyInfo.State == KEY_UP)
			{
				key->KeyInfo.State = KEY_DOWN;
				//fifo_put(&keyfifo, (unsigned char *)&key->KeyInfo, 1);
				FifoForKey.Put((unsigned char *)&key->KeyInfo, 1);
				/* 发送按钮按下的消息 */
				printf("post push info! \n");
			}
			
			if (key->LongTimeLimit > 0)						//按键按下持续时间, 0 表示不检测长按
			{				
				if (key->LongCount < key->LongTimeLimit)
				{
					/* 发送按钮持续按下的消息 */
					if ( ++key->LongCount == key->LongTimeLimit)
					{
						/* 键值放入按键 FIFO */
						key->KeyInfo.State = KEY_LONG;
						//fifo_put(&keyfifo, (unsigned char *)&key->KeyInfo, 1);
						FifoForKey.Put((unsigned char *)&key->KeyInfo, 1);
						printf("post continue push info! \n");						
					}
				}
				else
				{
					if (key->RepeatTimeLimit > 0)			//连续按键周期 
					{
						if (++key->RepeatCount >= key->RepeatTimeLimit)
						{
							key->RepeatCount = 0;
							/* 常按键后，每隔 RepeatSpeed * 10ms 发送 1 个按键 */
							//fifo_put(&keyfifo, (unsigned char *)&key->KeyInfo, 1);
							FifoForKey.Put((unsigned char *)&key->KeyInfo, 1);
							printf("post push interval info! \n");
						}
					}
				}
			}
		}
	}
	else
	{
		if(key->ShortCount > key->ShortTimeLimit)
		{
			key->ShortCount = key->ShortTimeLimit;
		}
		else if(key->ShortCount != 0)
		{
			key->ShortCount--;
		}
		else
		{
			if (key->KeyInfo.State != KEY_UP)
			{
				key->KeyInfo.State = KEY_UP;
				//fifo_put(&keyfifo, (unsigned char *)&key->KeyInfo, 1);
				FifoForKey.Put((unsigned char *)&key->KeyInfo, 1);
				/* 发送按钮弹起的消息 */
				printf("post up info! \n");
			}
		}
		key->LongCount = 0;
		key->RepeatCount = 0;
	}
}


//10ms调用
void KeyHandle(void)
{
	struct KEY_T *pKey = NULL;
	
	pKey = KeyList;
	
	while(pKey != NULL)
	{
		KeyCheck(pKey);
		pKey = pKey->next;
	}
}

//获取按键信息
char GetKeyInfo(KeyInfo_t *KeyInfo)
{
	if(FifoForKey.Get((unsigned char *)KeyInfo, 1))
		return 1;
	else
		return 0;
}




